package 曾世昌12_20.demo5;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.*;

public class test {
    static Scanner scanner = new Scanner( System.in );
    static SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd" );


    public static void main(String[] args) throws Exception {
        while (true) {
            System.out.println( "：1.账号登陆   2.游客浏览" );
            String s = scanner.nextLine();
            switch (s) {
                case "1":
                    login();
                    break;
                case "2":
                    otherLogin();
                    break;
                default:
                    System.out.println( "请输入正确浏览方式！" );
                    break;
            }
        }
    }

    public static boolean login() throws Exception {
        int a = 0;
        HashMap<String, String> map = new HashMap<>();
        System.out.println( "请输入用户名：" );
        String name = scanner.nextLine();
        System.out.println( "请输入密码：" );
        String psw = scanner.nextLine();
        Properties properties = new Properties();
        InputStreamReader inputStreamReader = new InputStreamReader( new FileInputStream( "E:\\untitled5\\src\\rc\\demo12_20\\demo5\\user.properties" ), "utf-8" );
        properties.load( inputStreamReader );
        inputStreamReader.close();
        Set<String> strings = properties.stringPropertyNames();
        for (String string : strings) {
            map.put( string, properties.getProperty( string ) );
        }
        for (Map.Entry<String, String> entry : map.entrySet()) {
            if (entry.getKey().equals( name ) && entry.getValue().equals( psw )) {
                a++;
            }
        }
        System.out.println( map );
        if (a != 0) {
            System.out.println();
            FileUtils.writeStringToFile( new File( "data.txt" ), name + sdf.format( new Date() ) + "登录成功！\r\n", true );
            return true;
        }
        FileUtils.writeStringToFile( new File( "data.txt" ), name + sdf.format( new Date() ) + "登录失败！\r\n", true );
        return false;
    }

    public static void otherLogin() throws IOException {
        FileUtils.writeStringToFile( new File( "data.txt" ), "游客" + sdf.format( new Date() ) + "进行浏览！\r\n", true );
    }
}
